<!DOCTYPE html>
<html>
<head>
    <title>添加药品</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>
    <script type="text/javascript" src="../../Js/ckeditor/ckeditor.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#backid').click(function () {
                window.location.href = "index.html";
            });
        });
    </script>
</head>
<body>
<form action="" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <th colspan="2" style="text-align: center; font-size: 20px">药品库存添加</th>
        </tr>
        <tr>
            <td width="10%" class="tableleft">药品编号</td>
            <td name="drId"></td>
        </tr>

        <tr>
            <td width="10%" class="tableleft">药品名称</td>
            <td name="drName"></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">剩余数量</td>
            <td name="drNumber"></td>
        </tr>

        <tr>
            <td width="10%" class="tableleft">添加数量</td>
            <td>
                <input type="text" value="" name="addNum"/>
            </td>
        </tr>


        <tr>
            <td colspan="2">
                <center>
                    <button type="button" class="btn btn-primary" id="submit">保存</button> &nbsp;&nbsp;
                    <button type="button" class="btn btn-success" id="returnList">返回列表</button>
                </center>
            </td>
        </tr>

    </table>
</form>
</body>

<script type="text/javascript">

    String.prototype.GetValue = function (para) {
        let reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?") + 1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    $(function () {
        let url = document.location.toString();
        let drId = url.GetValue("drId");
        let pageNum = url.GetValue("pageNum");

        if (drId !== "") {
            queryById(drId);
        }

        //返回
        $('#returnList').click(function () {
            window.location.href = "index.html?pageNum=" + pageNum;
        });

        //修改
        $('#submit').click(function () {

            let addNum = $("input[name='addNum']").val();

            //验证数值
            if(isNaN(addNum) || addNum === ""){
                alert("请填写正确的信息！");
            }else{
                $.ajax({
                    type: "POST",
                    url: "/drug/"+addNum,
                    data: $("form").serialize() + "&_method=PUT" + "&drId=" + drId,
                    dataType: "json",
                    success: function (vo) {
                        if (vo.code == 200) {
                            alert("添加成功！");
                            window.location.href = "index.html?pageNum=" + pageNum;
                        } else {
                            alert("更新失败！" + vo.msg);
                        }
                    }
                });
            }

        });
    });

    function queryById(drId) {
        $.ajax({
            type: "GET",
            url: "/drug/" + drId, //RESTful风格的API定义
            data: "",
            success: function (vo) {
                let obj = vo.obj;
                $("td[name='drId']").html(obj.drId);
                $("td[name='drName']").html(obj.drName);
                $("td[name='drNumber']").html(obj.drNumber==null?"无货":obj.drNumber);
            }
        });
    }

</script>

</html>